#定义矩阵类
class Matrix():  # 定义矩阵类
    def __init__(self, list, m, n):  # m row * n column 用数组EigenArray来代表这个矩阵的特征，实现运算
        EigenArray = []
        Row = []
        for r in range(len(list)):
            Row.append(list[r])
            if r % n == n - 1:
                EigenArray.append(Row)
                Row = []
        self.EigenArray = EigenArray
        self.m = m
        self.n = n

    def matrix(self, r, c):  # 打印矩阵中第r行第c列（都是从0开始计数）
        return self.EigenArray[r][c]

    # def LinearArray(self):  # 转换为一个线性的数组，方便处理
    #     LinearArray = []  # 命名重复？？？
    #     for i in range(self.m):
    #         for j in range(self.n):
    #             LinearArray.append(self.matrix(i, j))
    #     return LinearArray

    def blocks(self, sl, k):  # 分块，每个子矩阵的为sl*sl,输出第k块子矩阵
        assert self.n % sl == 0 and self.m % sl == 0, '不能划分'  # 判断能否划分
        j_k = k % (self.n // sl)
        i_k = k // (self.n // sl)
        rangei = range(i_k * sl, i_k * sl + sl)
        rangej = range(j_k * sl, j_k * sl + sl)
        blocks = []
        for i in rangei:
            for j in rangej:
                blocks.append(self.matrix(i, j))
        return blocks

    def transform(self, func):  # 用方法func对矩阵进行操作
        EigenArray2 = self.EigenArray
        for r in range(len(EigenArray2)):
            for c in range(len(EigenArray2[0])):
                EigenArray2[r][c] = func(r, c, self.EigenArray)
        self.EigenArray = EigenArray2
        return self

    def division(self1, self2):  # 除以另一个矩阵
        for r in range(len(self1.EigenArray)):
            for c in range(len(self1.EigenArray[0])):
                self1.EigenArray[r][c] = self1.EigenArray[r][c] // self2.EigenArray[r][c]
        return self1

    def ZigZag(self):  # 再调试一个更好的法子（）（）（）          #ZigZag操作
        ZigZag = []
        r = 0
        c = 0
        ZigZag.append(self.matrix(r, c))
        for i in range(3):
            c += 1
            ZigZag.append(self.matrix(r, c))
            for j in range(2 * i + 1):
                r += 1
                c -= 1
                ZigZag.append(self.matrix(r, c))
            r += 1
            ZigZag.append(self.matrix(r, c))
            for j in range(2 * i + 2):
                r -= 1
                c += 1
                ZigZag.append(self.matrix(r, c))
        c += 1
        ZigZag.append(self.matrix(r, c))
        for i in range(7):
            r += 1
            c -= 1
            ZigZag.append(self.matrix(r, c))
        c += 1
        ZigZag.append(self.matrix(r, c))
        for i in range(3):
            for j in range(6 - 2 * i):
                r -= 1
                c += 1
                ZigZag.append(self.matrix(r, c))
            r += 1
            ZigZag.append(self.matrix(r, c))
            for j in range(5 - 2 * i):
                r += 1
                c -= 1
                ZigZag.append(self.matrix(r, c))
            c = c + 1
            ZigZag.append(self.matrix(r, c))
        r -= 1
        return ZigZag  # 输出ZigZag列
